// pages/search/search.js
import {
  request
} from '../../request/index'
Page({
  data: {
    goods: [],
    //取消按钮是否显示
    isFocus: false,
    inputValue: ''
  },
  TimeId: -1,
  handleInput(e) {
    //获取输入框得值
    console.log(e.detail);
    const {
      value
    } = e.detail
    //检查
    if (!value.trim()) {
      this.setData({
        goods: [],
        isFocus: false
      })
      // 值是不合法的;
      return;
    }
    //发送请求
    this.setData({
      isFocus: true
    })

    clearTimeout(this.TimeId)

    this.TimeId = setTimeout(() => {
      this.qsearch(value)
    }, 1000)
  },
  // 发送请求获取搜索结果
  async qsearch(query) {
    const res = await request({
      url: "/goods/qsearch",
      data: {
        query
      }
    })
    this.setData({
      goods: res
    })
  },
  handleCancel() {
    this.setData({
      inputValue: "",
      isFocus: false,
      goods: []
    })
  }
})